using System;
using System.Drawing;
using System.IO;
using System.Text;
namespace Circus.CodeEditor.Serialization
{
	public class HtmlExport : FmtExport
	{
		private const string htmlB = "b";
		private const string htmlI = "i";
		private const string htmlU = "u";
		private const string htmlStrike = "strike";
		private const string htmlHtml = "html";
		private const string htmlBody = "body";
		private const string htmlHead = "head";
		private const string htmlTitle = "title";
		private const string htmlFont = "font";
		private const string htmlName = "name";
		private const string htmlColor = "color";
		private const string htmlBr = "br";
		private const string htmlLt = "&lt;";
		private const string htmlQt = "&gt;";
		private const string htmlAmp = "&amp;";
		private const string htmlQuot = "&quot;";
		private const string htmlNbsp = "&nbsp;";
		private bool colorChanged = false;
		private StringBuilder rBuilder;
		private void WriteTag(string tag)
		{
			this.writer.Write("<" + tag + ">");
		}
		private void WriteTag(string tag, string name, string value)
		{
			this.writer.Write(string.Concat(new object[]
			{
				"<",
				tag,
				' ',
				name,
				"=",
				value,
				">"
			}));
		}
		private void WriteEndTag(string tag)
		{
			this.writer.Write("</" + tag + ">");
		}
		private string ColorToHtml(Color color)
		{
			return "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
		}
		private string StringToHtml(int pos, string str)
		{
			string result;
			if (str == string.Empty || str == null)
			{
				result = str;
			}
			else
			{
				this.rBuilder.Length = 0;
				bool flag = true;
				int i = 0;
				while (i < str.Length)
				{
					char c = str[i];
					char c2 = c;
					if (c2 <= '"')
					{
						if (c2 != '\t')
						{
							switch (c2)
							{
							case ' ':
								if (flag)
								{
									this.rBuilder.Append("&nbsp;");
								}
								else
								{
									this.rBuilder.Append(c);
								}
								break;
							case '!':
								goto IL_18A;
							case '"':
								this.rBuilder.Append("&quot;");
								break;
							default:
								goto IL_18A;
							}
						}
						else
						{
							int num = (this.edit != null) ? (this.edit.Lines.GetTabStop(pos) - pos) : EditConsts.DefaultTabStop;
							if (flag)
							{
								for (int j = 0; j < num; j++)
								{
									this.rBuilder.Append("&nbsp;");
								}
							}
							else
							{
								this.rBuilder.Append(new string(' ', num));
							}
							pos += num;
						}
					}
					else
					{
						if (c2 != '&')
						{
							switch (c2)
							{
							case '<':
								this.rBuilder.Append("&lt;");
								break;
							case '=':
								goto IL_18A;
							case '>':
								this.rBuilder.Append("&gt;");
								break;
							default:
								goto IL_18A;
							}
						}
						else
						{
							this.rBuilder.Append("&amp;");
						}
					}
					IL_19A:
					if (c != ' ' && c != '\t')
					{
						flag = false;
					}
					if (c != '\t')
					{
						pos++;
					}
					i++;
					continue;
					IL_18A:
					this.rBuilder.Append(c);
					goto IL_19A;
				}
				result = this.rBuilder.ToString();
			}
			return result;
		}
		protected override void WriteForeColor(Color foreColor)
		{
			if (this.colorChanged)
			{
				this.WriteEndTag("font");
			}
			this.WriteTag("font", "color", this.ColorToHtml(foreColor));
			this.colorChanged = true;
		}
		protected override void WriteBackColor(Color backColor)
		{
		}
		protected override void RemoveFontStyle(FontStyle style)
		{
			switch (style)
			{
			case FontStyle.Bold:
				this.WriteEndTag("b");
				break;
			case FontStyle.Italic:
				this.WriteEndTag("i");
				break;
			case FontStyle.Bold | FontStyle.Italic:
				break;
			case FontStyle.Underline:
				this.WriteEndTag("u");
				break;
			default:
				if (style == FontStyle.Strikeout)
				{
					this.WriteEndTag("strike");
				}
				break;
			}
		}
		protected override void AddFontStyle(FontStyle style)
		{
			switch (style)
			{
			case FontStyle.Bold:
				this.WriteTag("b");
				break;
			case FontStyle.Italic:
				this.WriteTag("i");
				break;
			case FontStyle.Bold | FontStyle.Italic:
				break;
			case FontStyle.Underline:
				this.WriteTag("u");
				break;
			default:
				if (style == FontStyle.Strikeout)
				{
					this.WriteTag("strike");
				}
				break;
			}
		}
		protected override void WriteText(int pos, string text)
		{
			this.writer.Write(this.StringToHtml(pos, text));
		}
		public HtmlExport()
		{
			this.rBuilder = new StringBuilder();
		}
		public override void BeginWrite(TextWriter writer, object userData)
		{
			base.BeginWrite(writer, userData);
			this.WriteTag("html");
			writer.WriteLine();
			this.WriteTag("head");
			if (this.edit != null)
			{
				this.WriteTag("title");
				writer.Write(this.edit.Source.FileName);
				this.WriteEndTag("title");
			}
			this.WriteEndTag("head");
			writer.WriteLine();
			this.WriteTag("body");
			writer.WriteLine();
			if (this.edit != null)
			{
				this.WriteTag("font", "name", this.edit.Font.Name);
			}
			writer.WriteLine();
		}
		public override void EndWrite()
		{
			if (this.colorChanged)
			{
				this.WriteEndTag("font");
			}
			this.WriteFontStyle(FontStyle.Regular);
			this.WriteEndTag("font");
			this.writer.WriteLine();
			this.WriteEndTag("body");
			this.writer.WriteLine();
			this.WriteEndTag("html");
		}
		protected override void StartLine()
		{
			if (!this.firstLine)
			{
				this.writer.WriteLine();
				this.WriteTag("br");
			}
		}
	}
}
